﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Lottery.Herper;

using Lottery.MediaData.Core;

namespace Lottery.MediaData.UI {
    public partial class BatchMainForm : Form {
        public BatchMainForm() {
            InitializeComponent();

            label310.Text = string.Empty;
            labelYuCe.Text = string.Empty;
            labelPeiLv.Text = string.Empty;

            editCCT1.Items.Clear();
            editT2CC.Items.Clear();
            for (int i = 3; i < 15; i++) {
                editCCT1.Items.Add(i.ToString());
                editT2CC.Items.Add(i.ToString());
            }
            editCCT1.SelectedIndex = editCCT1.Items.Count - 1;
            editT2CC.SelectedIndex = editT2CC.Items.Count - 1;

            this.StartPosition = FormStartPosition.CenterScreen;
        }

        [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetForegroundWindow")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);//设置此窗体句柄的窗体为活动窗体

        /// <summary>
        /// 310数据
        /// </summary>
        private List<BatchDataItem> m_data;

        private List<BatchDataItemT2> m_dataT2;

        /// <summary>
        /// 预测数据
        /// </summary>
        private List<YuCeDataItem> m_ycData;

        /// <summary>
        /// 赔率数据
        /// </summary>
        private List<PeiLvDataItem> m_plData;

        private string m_fileName310 = string.Empty;

        private string m_fileNameYuCe = string.Empty;

        private string m_fileNamePeiLv = string.Empty;

        private void button1_Click(object sender, EventArgs e) {
            ofdExcel.FileName = string.Empty;
            if (ofdExcel.ShowDialog() != DialogResult.OK)
                return;

            m_fileName310 = ofdExcel.FileName;

            WaitForm form = new WaitForm();
            form.SetInfo("正在导入310数据Excel文件，请稍等...");

            BackgroundWorker bw = new BackgroundWorker();
            bw.DoWork += (a, b) => {
                m_data = BatchLoadExcel.Load(m_fileName310);
                m_dataT2 = new List<BatchDataItemT2>();
                foreach (var item in m_data) {
                    m_dataT2.Add(item.ToT2());
                }
            };

            bw.RunWorkerCompleted += (a, b) => {
                SetForegroundWindow(form.Handle);
                form.Hide();
                form.Close();
                if (b.Error == null) {
                    label310.Text = m_fileName310;
                    this.ShowInfo("成功导入310数据");
                } else {
                    this.ShowInfo(b.Error.Message);
                }
            };
            bw.RunWorkerAsync();
            form.ShowDialog();

        }

        private void button2_Click(object sender, EventArgs e) {
            // 调入预测数据
            ofdExcel.FileName = string.Empty;
            if (ofdExcel.ShowDialog() != DialogResult.OK) {
                return;
            }

            m_fileNameYuCe = ofdExcel.FileName;

            WaitForm form = new WaitForm();
            form.SetInfo("正在导入预测数据Excel文件，请稍等...");

            BackgroundWorker bw = new BackgroundWorker();
            bw.DoWork += (a, b) => {
                m_ycData = YuCeLoadExcel.Load(m_fileNameYuCe);
            };

            bw.RunWorkerCompleted += (a, b) => {
                SetForegroundWindow(form.Handle);
                form.Hide();
                form.Close();
                if (b.Error == null) {
                    labelYuCe.Text = m_fileNameYuCe;
                    this.ShowInfo("成功导入预测数据");
                } else {
                    this.ShowInfo(b.Error.Message);
                }
            };
            bw.RunWorkerAsync();
            form.ShowDialog();
        }

        private void button3_Click(object sender, EventArgs e) {
            // 调入赔率数据
            ofdExcel.FileName = string.Empty;
            if (ofdExcel.ShowDialog() != DialogResult.OK) {
                return;
            }

            m_fileNamePeiLv = ofdExcel.FileName;

            WaitForm form = new WaitForm();
            form.SetInfo("正在导入赔率数据Excel文件，请稍等...");

            BackgroundWorker bw = new BackgroundWorker();
            bw.DoWork += (a, b) => {
                m_plData = PeiLvLoadExcel.Load(m_fileNamePeiLv);
            };

            bw.RunWorkerCompleted += (a, b) => {
                SetForegroundWindow(form.Handle);
                form.Hide();
                form.Close();
                if (b.Error == null) {
                    labelPeiLv.Text = m_fileNamePeiLv;
                    this.ShowInfo("成功导入赔率数据");
                } else {
                    this.ShowInfo(b.Error.Message);
                }
            };
            bw.RunWorkerAsync();
            form.ShowDialog();
        }

        /// <summary>
        /// 检查数据
        /// </summary>
        /// <returns></returns>
        private bool CheckData() {
            if (m_data.Count != m_ycData.Count) {
                var info = string.Format("{0}与{1}的数据行数不相等", m_fileName310, m_fileNameYuCe);
                this.ShowInfo(info);
                return false;
            }

            if (m_ycData.Count != m_plData.Count) {
                var info = string.Format("{0}与{1}的数据行数不相等", m_fileNamePeiLv, m_fileNameYuCe);
                this.ShowInfo(info);
                return false;
            }

            // 检查序号是否一一对应
            for (int i = 0; i < m_data.Count; i++) {
                var data = m_data[i];
                var ycData = m_ycData[i];
                var plData = m_plData[i];
                if (data.Order != ycData.Order) {
                    var info = string.Format("预测次数号为 {0} 的数据在{1}中没有", data.Order, m_fileNameYuCe);
                    this.ShowInfo(info);
                    return false;
                }

                if (data.Order != plData.Order) {
                    var info = string.Format("预测次数号为 {0} 的数据在{1}中没有", data.Order, m_fileNamePeiLv);
                    this.ShowInfo(info);
                    return false;
                }
            }

            if (editT2CC.SelectedIndex == -1) {
                this.ShowInfo("没有选择场次");
                return false;
            }

            return true;
        }

        private void button4_Click(object sender, EventArgs e) {
            // 表一数据合并
            if (string.IsNullOrEmpty(m_fileName310)) {
                this.ShowInfo("没有调入14场数据");
                return;
            }

            if (string.IsNullOrEmpty(m_fileNameYuCe)) {
                this.ShowInfo("没有调入预测数据");
                return;
            }

            if (string.IsNullOrEmpty(m_fileNamePeiLv)) {
                this.ShowInfo("没有调入赔率数据");
                return;
            }

            if (!CheckData()) return;

            var ccCount = Convert.ToInt32(editCCT1.Items[editCCT1.SelectedIndex].ToString());

            var list = Batch.CombineT1(m_data, m_plData, ccCount);
            editT1.Items.Clear();
            editFrom1.Items.Clear();
            editTo1.Items.Clear();
            editFrom2.Items.Clear();
            editTo2.Items.Clear();
            foreach (var item in list) {
                editT1.Items.Add(item);
                editFrom1.Items.Add(item);
                editTo1.Items.Add(item);
                editFrom2.Items.Add(item);
                editTo2.Items.Add(item);
            }
        }

        private bool CheckT1() {
            if (editT1.Items.Count == 0) {
                this.ShowInfo("没有进行数据合并操作");
                return false;
            }

            if (editFrom1.SelectedIndex == -1) {
                editFrom1.Focus();
                this.ShowInfo("没有选择区域1的小数");
                return false;
            }

            if (editTo1.SelectedIndex == -1) {
                editTo1.Focus();
                this.ShowInfo("没有选择区域1的大数");
                return false;
            }

            if (editTo1.SelectedIndex >= editFrom1.SelectedIndex) {
                editTo1.Focus();
                this.ShowInfo("区域1设置不正确");
                return false;
            }

            if (editFrom2.SelectedIndex == -1) {
                editFrom2.Focus();
                this.ShowInfo("没有选择区域2的小数");
                return false;
            }

            if (editTo2.SelectedIndex == -1) {
                editTo2.Focus();
                this.ShowInfo("没有选择区域2的大数");
                return false;
            }

            if (editTo2.SelectedIndex >= editFrom2.SelectedIndex) {
                editTo2.Focus();
                this.ShowInfo("区域2设置不正确");
                return false;
            }

            var from1 = editFrom1.SelectedIndex;
            if (from1 <= editFrom2.SelectedIndex && from1 >= editTo2.SelectedIndex) {
                editFrom1.Focus();
                this.ShowInfo("区域1和区域2重合");
                return false;
            }

            var from2 = editFrom2.SelectedIndex;
            if (from2 <= editFrom1.SelectedIndex && from2 >= editTo1.SelectedIndex) {
                editFrom1.Focus();
                this.ShowInfo("区域1和区域2重合");
                return false;
            }

            if (editCCT1.SelectedIndex == -1) {
                editCCT1.Focus();
                this.ShowInfo("没有选择场次");
                return false;
            }

            return true;
        }

        private void button5_Click(object sender, EventArgs e) {
            // 表一 生成统计表
            if (!CheckT1()) {
                return;
            }

            sfdExcel.FileName = string.Empty;
            if (sfdExcel.ShowDialog() != DialogResult.OK) {
                return;
            }

            var fileName = sfdExcel.FileName;
            WaitForm form = new WaitForm();
            form.SetInfo("正在生成表一的统计表，请稍等...");

            var from1 = editFrom1.Text;
            var to1 = editTo1.Text;
            var from2 = editFrom2.Text;
            var to2 = editTo2.Text;
            var ccCountStr = editCCT1.Text;
            BackgroundWorker bw = new BackgroundWorker();
            bw.DoWork += (a, b) => {
                ExportExecelT1(fileName, from1, to1, from2, to2, ccCountStr);
            };

            bw.RunWorkerCompleted += (a, b) => {
                form.Hide();
                form.Close();
                if (b.Error != null) {
                    this.ShowInfo(b.Error.Message);
                }
            };
            bw.RunWorkerAsync();
            form.ShowDialog();
        }

        /// <summary>
        /// 为表一生成统计表
        /// </summary>
        /// <param name="fileName">生成的Excel文件名</param>
        private void ExportExecelT1(string fileName, string from1, string to1, string from2, 
            string to2, string ccCountStr) {
            var igData = new List<string>();
            for (int i = 0; i < editT1.CheckedItems.Count; i++) {
                igData.Add(editT1.CheckedItems[i].ToString());
            }

            var c = new CL();
            c.From1 = Convert.ToInt32(from1);
            c.To1 = Convert.ToInt32(to1);
            c.From2 = Convert.ToInt32(from2);
            c.To2 = Convert.ToInt32(to2);

            var md = new List<string>();
            for (int i = 0; i < editT1.Items.Count; i++) {
                md.Add(editT1.Items[i].ToString());
            }
            md.Add("合并数=1合并");
            md.Add("合并数=2合并");
            md.Add("合并数=3合并");
            md.Add("合并数=1+2合并");
            md.Add("合并数=1+2+3合并");
            md.Add(string.Format("{0}-{1}合并", c.From1, c.To1));
            md.Add(string.Format("{0}-{1}合并", c.From2, c.To2));

            var ccCount = Convert.ToInt32(ccCountStr);

            var data = Batch.GenT1(m_data, m_ycData, m_plData, igData, c, ccCount);

            var xhList = new List<string>(); // 序号
            foreach (var item in m_data) {
                xhList.Add(item.Order);
            }

            Batch.SaveExcelT1(fileName, data, xhList, md);
        }

        private void button6_Click(object sender, EventArgs e) {
            // 表二 数据合并
            if (string.IsNullOrEmpty(m_fileName310)) {
                this.ShowInfo("没有调入14场数据");
                return;
            }

            if (string.IsNullOrEmpty(m_fileNameYuCe)) {
                this.ShowInfo("没有调入预测数据");
                return;
            }

            if (string.IsNullOrEmpty(m_fileNamePeiLv)) {
                this.ShowInfo("没有调入赔率数据");
                return;
            }

            if (!CheckData()) return;

            var ccCount = Convert.ToInt32(editT2CC.Items[editT2CC.SelectedIndex].ToString());

            var list = Batch.CombineT2(m_dataT2, m_plData, ccCount);
            editT2.Items.Clear();
            editT2From1.Items.Clear();
            editT2To1.Items.Clear();
            editT2From2.Items.Clear();
            editT2To2.Items.Clear();
            foreach (var item in list) {
                editT2.Items.Add(item);
                editT2From1.Items.Add(item);
                editT2To1.Items.Add(item);
                editT2From2.Items.Add(item);
                editT2To2.Items.Add(item);
            }

        }

        private void button7_Click(object sender, EventArgs e) {
            // 表二 生成统计表
            if (!CheckT2()) {
                return;
            }

            sfdExcel.FileName = string.Empty;
            if (sfdExcel.ShowDialog() != DialogResult.OK) {
                return;
            }

            var fileName = sfdExcel.FileName;
            var igData = new List<string>();
            for (int i = 0; i < editT2.CheckedItems.Count; i++) {
                igData.Add(editT2.CheckedItems[i].ToString());
            }

            var c = new CL();
            c.From1 = Convert.ToInt32(editT2From1.Text);
            c.To1 = Convert.ToInt32(editT2To1.Text);
            c.From2 = Convert.ToInt32(editT2From2.Text);
            c.To2 = Convert.ToInt32(editT2To2.Text);

            var md = new List<string>();
            for (int i = 0; i < editT2.Items.Count; i++) {
                md.Add(editT2.Items[i].ToString());
            }
            md.Add("合并数=1合并");
            md.Add("合并数=2合并");
            md.Add("合并数=3合并");
            md.Add("合并数=1+2合并");
            md.Add("合并数=1+2+3合并");
            md.Add(string.Format("{0}-{1}合并", c.From1, c.To1));
            md.Add(string.Format("{0}-{1}合并", c.From2, c.To2));

            var ccCount = Convert.ToInt32(editT2CC.Text);

            WaitForm form = new WaitForm();
            form.SetInfo("正在生成表二的统计表，请稍等...");

            BackgroundWorker bw = new BackgroundWorker();
            bw.DoWork += (a, b) => {
                ExportExecelT2(fileName, igData, c, md, ccCount);
            };

            bw.RunWorkerCompleted += (a, b) => {
                form.Hide();
                form.Close();
                if (b.Error != null) {
                    this.ShowInfo(b.Error.Message);
                }
            };
            bw.RunWorkerAsync();
            form.ShowDialog();
        }

        /// <summary>
        /// 为表二生成统计表
        /// </summary>
        /// <param name="fileName">生成的Excel文件名</param>
        private void ExportExecelT2(string fileName, List<string> igData,
            CL c, List<string> md, int ccCount) {
            var data = Batch.GenT2(m_dataT2, m_ycData, m_plData, igData, c, ccCount);

            var xhList = new List<string>(); // 序号
            foreach (var item in m_data) {
                xhList.Add(item.Order);
            }

            Batch.SaveExcelT2(fileName, data, xhList, md);
        }

        private bool CheckT2() {
            if (editT2.Items.Count == 0) {
                this.ShowInfo("没有进行数据合并操作");
                return false;
            }

            if (editT2From1.SelectedIndex == -1) {
                editT2From1.Focus();
                this.ShowInfo("没有选择区域1的小数");
                return false;
            }

            if (editT2To1.SelectedIndex == -1) {
                editT2To1.Focus();
                this.ShowInfo("没有选择区域1的大数");
                return false;
            }

            if (editT2To1.SelectedIndex >= editT2From1.SelectedIndex) {
                editT2To1.Focus();
                this.ShowInfo("区域1设置不正确");
                return false;
            }

            if (editT2From2.SelectedIndex == -1) {
                editT2From2.Focus();
                this.ShowInfo("没有选择区域2的小数");
                return false;
            }

            if (editT2To2.SelectedIndex == -1) {
                editT2To2.Focus();
                this.ShowInfo("没有选择区域2的大数");
                return false;
            }

            if (editT2To2.SelectedIndex >= editT2From2.SelectedIndex) {
                editT2To2.Focus();
                this.ShowInfo("区域2设置不正确");
                return false;
            }

            var from1 = editT2From1.SelectedIndex;
            if (from1 <= editT2From2.SelectedIndex && from1 >= editT2To2.SelectedIndex) {
                editT2From1.Focus();
                this.ShowInfo("区域1和区域2重合");
                return false;
            }

            var from2 = editT2From2.SelectedIndex;
            if (from2 <= editT2From1.SelectedIndex && from2 >= editT2To1.SelectedIndex) {
                editT2From1.Focus();
                this.ShowInfo("区域1和区域2重合");
                return false;
            }

            if (editT2CC.SelectedIndex == -1) {
                editT2CC.Focus();
                this.ShowInfo("没有选择场次");
                return false;
            }

            return true;
        }

        private void editCCT1_SelectedIndexChanged(object sender, EventArgs e) {
            editT1.Items.Clear();
            editFrom1.Items.Clear();
            editTo1.Items.Clear();
            editFrom2.Items.Clear();
            editTo2.Items.Clear();
        }

        private void editT2CC_SelectedIndexChanged(object sender, EventArgs e) {
            editT2.Items.Clear();
            editT2From1.Items.Clear();
            editT2To1.Items.Clear();
            editT2From2.Items.Clear();
        }
    }
}
